package com.sikaryofficial.dataindicator.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.common.core.web.domain.PageResult;
import com.sikaryofficial.dataindicator.domain.dto.req.BrowseDataPageRequest;
import com.sikaryofficial.dataindicator.domain.dto.req.UserBehaviorDataPageRequest;
import com.sikaryofficial.dataindicator.domain.dto.resp.BrowseDataResponse;
import com.sikaryofficial.dataindicator.domain.dto.resp.UserBehaviorDataResponse;
import com.sikaryofficial.dataindicator.domain.eo.BrowseDataEO;
import com.sikaryofficial.dataindicator.domain.eo.UserBehaviorEO;
import com.sikaryofficial.dataindicator.manager.BuriedPointDataManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 埋点数据查询
 */
@RestController
@RequestMapping("/buriedPointData")
@Api(tags = "运营-埋点数据查询")
@Slf4j
public class BuriedPointDataReportController {

    @Resource
    private BuriedPointDataManager buriedPointDataManager;

    @ApiOperation("分页查询浏览记录")
    @PostMapping("/pageBrowseData")
    public PageResult<BrowseDataResponse> pageBrowseData(@RequestBody BrowseDataPageRequest req) {
        return buriedPointDataManager.pageBrowseData(req);
    }


    @ApiOperation("分页查询用户行为数据")
    @PostMapping("/userBehaviorData")
    public PageResult<UserBehaviorDataResponse> pageUserBehaviorData(@RequestBody UserBehaviorDataPageRequest req) {
        return buriedPointDataManager.pageUserBehaviorData(req);
    }

    @ApiOperation("导出浏览记录")
    @PostMapping("/exportBrowseData")
    public void exportBrowseData(HttpServletResponse response, @RequestBody BrowseDataPageRequest req) {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("UTF-8");
            String fileName = "浏览记录导出" + LocalDateTimeUtil.format(LocalDateTime.now(), DateTimeFormatter.ofPattern(DatePattern.PURE_DATETIME_FORMAT.getPattern()));
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcelFactory.write(response.getOutputStream())
                    .head(BrowseDataEO.class)
                    .sheet()
                    .doWrite(buriedPointDataManager.exportBrowseData(req));
        } catch (Exception e) {
            log.error("exportBrowseData", e);
            throw new ServiceException("exportBrowseData error.");
        }
    }

    @ApiOperation("导出用户行为记录")
    @PostMapping("/exportUserBehaviorData")
    public void exportUserBehaviorData(HttpServletResponse response, @RequestBody UserBehaviorDataPageRequest req) {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("UTF-8");
            String fileName = "用户行为记录导出" + LocalDateTimeUtil.format(LocalDateTime.now(), DateTimeFormatter.ofPattern(DatePattern.PURE_DATETIME_FORMAT.getPattern()));
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcelFactory.write(response.getOutputStream())
                    .head(UserBehaviorEO.class)
                    .sheet()
                    .doWrite(buriedPointDataManager.exportUserBehaviorData(req));
        } catch (Exception e) {
            log.error("exportUserBehaviorData", e);
            throw new ServiceException("exportUserBehaviorData error.");
        }
    }
}
